<script type="text/javascript">
	validation_promotions = null;
	promotionslist_createimageWindows = null;
	$(document).ready(function () {
		/* DECLARATION VARIABLE */
		/* General Variable */
		var theme = 'fresh';
		var promotionslist_source;
		var promotionslist_dataAdapter;

		/* Area and Field Variable */
		var promotionslist_menu = $("#promotionslist_menu");
		var promotionslist_content = $("#promotionslist_content");
		var promotionslist_expander = $("#promotionslist_expander");
		var promotionslist_form = $("#promotionslist_form");
		var promotionslist_gridview = $("#promotionslist_gridview");
		var promotionslist_search_textfield = $("#promotionslist_search_textfield");
		var promotionslist_searchbutton = $("#promotionslist_searchbutton");
		var promotionslist_addbutton = $("#promotionslist_addbutton");
		var promotionslist_editbutton = $("#promotionslist_editbutton");
		var promotionslist_deletebutton = $("#promotionslist_deletebutton");
		var promotionslist_resetbutton = $("#promotionslist_resetbutton");

		/* END DECLARATION VARIABEL */

		/* SET TYPE AND THEME */
		$('.field_input').addClass('jqx-input');
		$('.field_input').addClass('field');
		$('.field_input').addClass('jqx-rc-all');

		promotionslist_menu.jqxMenu({ align: 'left', width: '800px', height: '27px', autoOpen: false, autoCloseOnMouseLeave: false, showTopLevelArrows: true, theme: theme });
		promotionslist_menu.css('vdeskripsibility', 'vdeskripsible');
		promotionslist_addbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });
		promotionslist_editbutton.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		promotionslist_deletebutton.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		promotionslist_searchbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });
		promotionslist_resetbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });

		var promotionslist_source = {
			id : 'id',
			datatype : "json",
			url : "<?php echo base_url(); ?>c_promotions/promotions_list",
			root : "results",
			datafields : [
				{ name : 'id' },
				{ name : 'image' },
				{ name : 'judul' },
				{ name : 'deskripsi' },
				{ name : 'link' }
			],
			beforeprocessing : function(data){
				promotionslist_source.totalrecords = data[0].TotalRows;
			}
		}
		var promotionslist_dataAdapter = new $.jqx.dataAdapter(promotionslist_source);

		var imagerenderer = function (row, datafield, value) {
                return '<img style="margin: 5px;" height="100" width="100" src="<?php echo base_url();?>' + value + '"/>';
        }

		promotionslist_gridview.jqxGrid({
			source : promotionslist_dataAdapter,
			width : 800,
			theme : theme,
			pageable : true,
			autoheight : true,
			sortable : true,
			filterable : true,
			columnsresize : true,
			virtualmode : true,
			pagesize : 5,
			rowsheight: 100,
			pagesizeoptions : [15],
			rendergridrows : function(){
				return promotionslist_dataAdapter.records;
			},
			columns : [
				{text : 'Gambar', dataField : 'image', width : 200, cellsrenderer: imagerenderer},
				{text : 'Judul', dataField : 'judul', width: 200},
				{text : 'Link', dataField : 'link', width: 300}
			]
		});




		/* DECLARATION FUNCTION */
		function reset_form(){
		}
		function refresh_grid(){
			promotionslist_gridview.jqxGrid('updatebounddata');
		}

		promotionslist_addbutton.click(function(){
			$('#promotionslist_content').empty();
			$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#promotionslist_content').load('index.php/c_promotions/promotions_addedit');
		});

		promotionslist_editbutton.click(function(){
			var promotions_id = '';
			var rowindex = promotionslist_gridview.jqxGrid('getselectedrowindex');
			var data_record = promotionslist_gridview.jqxGrid('getrowdata', rowindex);
			if(data_record !== null){
				promotions_id = data_record.id;
				$('#promotionslist_content').empty();
				$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
				$('#promotionslist_content').load('index.php/c_promotions/promotions_addedit/'+promotions_id);
			}else{
				alert("Pilih salah satu field yang ingin diubah");
			}
		});

		promotionslist_searchbutton.click(function(){
			var search_text = promotionslist_search_textfield.val();
			promotionslist_source['url'] = "index.php/c_promotions/promotions_list?query=" + search_text;
			promotionslist_gridview.jqxGrid('gotopage',0);
			promotionslist_dataAdapter.dataBind();
		});
		promotionslist_resetbutton.click(function(){
			promotionslist_search_textfield.val("");
			promotionslist_source['url'] = "index.php/c_promotions/promotions_list/";
			promotionslist_gridview.jqxGrid('gotopage',0);
			promotionslist_dataAdapter.dataBind();
		});

		promotionslist_deletebutton.click(function(){

			var rowindex = promotionslist_gridview.jqxGrid('getselectedrowindex');
			var data_record = promotionslist_gridview.jqxGrid('getrowdata', rowindex);
			if(data_record !== null){
			var promotions_id=data_record.id;

			if(window.confirm("Apakah anda yakin menghapus data ini ? ")){
				$(document).ready(function () {
					$.ajax({
						url : 'index.php/c_promotions/promotions_delete',
						type : 'POST',
						data : {
							id : promotions_id
						}, success : function(msg){
							var result = eval(msg);
							if(result == 1){
								$("#promotionslist_gridview").jqxGrid('updatebounddata');
								alert("Data sukses dihapus");
							}else{
								alert("Data gagal dihapus");
							}
						}
					});

				});
			}
		}

		});

		function promotionslist_view(event, type ,file){
			promotionslist_createimageWindows(type ,file);
		}

		function promotionslist_edit(event, id) {
			self.location='index.php/c_promotions/get_action&task=update/' + id;
		}

});
</script>
<style type="text/css">
	.field{
		height : 20px;
	}
</style>
	<!-- Begin one column window -->
<br class="clear"/>
<div class="header">
	<span><h1>PROMOTIONS</h1></span>
</div>
<div id="promotionslist_content">
	<div id="promotionslist_menu">
		<div id='promotionslist_addbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/add.png' />
			<div style='float: left; margin-left: 4px;'>Baru</div>
		</div>
		<div id='promotionslist_editbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/page_edit.png' />
			<div style='float: left; margin-left: 4px;'>Ubah</div>
		</div>
		<div id='promotionslist_deletebutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/delete.png' />
			<div style='float: left; margin-left: 4px;'>Hapus</div>
		</div>
		<div style="float:left;">
			<input type="text" id="promotionslist_search_textfield" class="field_input" style="height: 24px; width : 200px; background-color : #FFFFFF;">
		</div>
		<div id='promotionslist_searchbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/search.gif' />
			<div style='float: left; margin-left: 4px;'>Cari</div>
		</div>
		<div id='promotionslist_resetbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/table_refresh.png' />
			<div style='float: left; margin-left: 4px;'>Reset</div>
		</div>
	</div>
	<div id="promotionslist_gridview"></div>
</div>